package org.automated.realestatemanager.advertisementgetter;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.parsers.AdvertisementParser;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class Kvartal extends AbstractAdvertisementGetter {

	private static final Logger log = Logger.getLogger(Kvartal.class);

	public void executeParcing(final AdvertisementParser advParser) {
		advParser.setSource("kvartal");
		final int pages = 50;

		try {
			Parser parser = null;
			for (int k = 0; k < pages; k++) {
				parser = new Parser(
						"http://www.kvartal.ua/kupit-kvartiru/page/"
								+ k
								+ "?action=getMetaByTerm&id_search=0&view="
								+ "table&type-operation=kupit"
								+ "&type_of_post=kvartira&tax%5B0%5D%5B0%5D=0"
								+ "&xydac_custom%5Btextint-custom_meta-obshhaya_ploshhad%5D%5Bfrom%5D"
								+ "&xydac_custom%5Btextint-custom_meta-obshhaya_ploshhad%5D%5Bto%5D"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-cena%5D%5Bfrom%5D=0"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-cena%5D%5Bto%5D=0"
								+ "&xydac_custom%5Bcombobox-custom_meta-kolichestvo_komnat%5D=0"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-etazh%5D%5Bfrom%5D=0"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-etazh%5D%5Bto%5D=0"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-etazhnost%5D%5Bfrom%5D=0"
								+ "&xydac_custom%5Bcomboboxint-custom_meta-etazhnost%5D%5Bto%5D=0"
								+ "&subdomain%5B0%5D=33&subdomain%5B1%5D=34&subdomain%5B2%5D=35"
								+ "&subdomain%5B3%5D=36&subdomain%5B4%5D=37&subdomain%5B5%5D=38"
								+ "&subdomain%5B6%5D=39&subdomain%5B7%5D=40&subdomain%5B8%5D=41"
								+ "&subdomain%5B9%5D=42&subdomain%5B10%5D=43&subdomain%5B11%5D=44"
								+ "&subdomain%5B12%5D=45&subdomain%5B13%5D=46&subdomain%5B14%5D=47"
								+ "&subdomain%5B15%5D=48&subdomain%5B16%5D=49&subdomain%5B17%5D=50"
								+ "&subdomain%5B18%5D=394&subdomain%5B19%5D=52&subdomain%5B20%5D=53"
								+ "&subdomain%5B21%5D=54&subdomain%5B22%5D=55&subdomain%5B23%5D=56"
								+ "&subdomain%5B24%5D=57&subdomain%5B25%5D=58&subdomain%5B26%5D=59"
								+ "&subdomain%5B27%5D=60&subdomain%5B28%5D=61&subdomain%5B29%5D=62"
								+ "&subdomain%5B30%5D=63&subdomain%5B31%5D=64&subdomain%5B32%5D=6"
								+ "5&subdomain%5B33%5D=66&subdomain%5B34%5D=67&subdomain%5B35%5D=68"
								+ "&subdomain%5B36%5D=69&subdomain%5B37%5D=70&subdomain%5B38%5D=71"
								+ "&subdomain%5B39%5D=72&subdomain%5B40%5D=73&subdomain%5B41%5D=74"
								+ "&subdomain%5B42%5D=75&subdomain%5B43%5D=76&subdomain%5B44%5D=77"
								+ "&subdomain%5B45%5D=78&subdomain%5B46%5D=93&subdomain%5B47%5D=94"
								+ "&subdomain%5B48%5D=95&subdomain%5B49%5D=96&subdomain%5B50%5D=97"
								+ "&subdomain%5B51%5D=98&subdomain%5B52%5D=99&subdomain%5B53%5D=100"
								+ "&subdomain%5B54%5D=101&subdomain%5B55%5D=102&subdomain%5B56%5D=103"
								+ "&subdomain%5B57%5D=104&subdomain%5B58%5D=105&subdomain%5B59%5D=107"
								+ "&subdomain%5B60%5D=106&subdomain%5B61%5D=108&subdomain%5B62%5D=109"
								+ "&subdomain%5B63%5D=110&subdomain%5B64%5D=111&subdomain%5B65%5D=112"
								+ "&subdomain%5B66%5D=113&subdomain%5B67%5D=114&subdomain%5B68%5D=464"
								+ "&subdomain%5B69%5D=115&subdomain%5B70%5D=116&subdomain%5B71%5D=117"
								+ "&subdomain%5B72%5D=118&subdomain%5B73%5D=119&subdomain%5B74%5D=120"
								+ "&subdomain%5B75%5D=79&subdomain%5B76%5D=80&subdomain%5B77%5D=81"
								+ "&subdomain%5B78%5D=82&subdomain%5B79%5D=83&subdomain%5B80%5D=84"
								+ "&subdomain%5B81%5D=85&subdomain%5B82%5D=86&subdomain%5B83%5D=87"
								+ "&subdomain%5B84%5D=88&subdomain%5B85%5D=89&subdomain%5B86%5D=90&"
								+ "subdomain%5B87%5D=91&subdomain%5B88%5D=92");

				parser.setEncoding("utf-8");

				final NodeFilter odd = new CssSelectorNodeFilter(".table_poisk");

				final NodeList nodeList = parser.parse(odd);
				for (int i = 0; i < nodeList.size(); i++) {

					final Node node = nodeList.elementAt(i);
					String n = node.toHtml();

					n = n.replaceAll("</?table[^<]{1,}>", " ");
					final String[] texts = n.split("</?tr[^<]{0,}>");
					log.debug("On page # " + 0 + " found " + texts.length
							+ " advertisements");
					for (String text : texts) {
						text = text.replaceAll("</?b[^>]*>", " ");
						text = text.replaceAll("</?strong[^>]*>", " ");
						text = text.replaceAll("(\\s*)?<[^>]*>(\\s*)?", " ");
						text = text.replaceAll("\\s/\\s", "/");
						// text = text.replaceAll("[\\s]{2,}", " ");
						log.debug("\n\n" + text);
						advParser.executeParcing(text);
					}
				}

				try {
					Thread.sleep(1000l);
				} catch (final InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} catch (final ParserException e) {
			e.printStackTrace();
		}
	}
}
